package com.lemon.boot.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * @author 李猛
 * @datetime 2023/10/10 14:30
 * @description 类对象
 */
@Slf4j
public class LocalDateUtil {
    public final static DateTimeFormatter DATE_FORMATTER_1 = DateTimeFormatter.ofPattern("yyyy-MM");
    public final static DateTimeFormatter DATE_FORMATTER_2 = DateTimeFormatter.ofPattern("yyyy年MM月");
    public final static DateTimeFormatter DATE_FORMATTER_3 = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    public final static DateTimeFormatter DATE_FORMATTER_4 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public final static DateTimeFormatter DATE_FORMATTER_5 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public final static DateTimeFormatter DATE_FORMATTER_6 = DateTimeFormatter.ofPattern("HH:mm:ss");
    public final static DateTimeFormatter DATE_FORMATTER_7 = DateTimeFormatter.ofPattern("HHmmss");
    public final static DateTimeFormatter DATE_FORMATTER_8 = DateTimeFormatter.ofPattern("yyyyMMdd");
    public final static DateTimeFormatter DATE_FORMATTER_9 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
    public final static DateTimeFormatter DATE_FORMATTER_10 = DateTimeFormatter.ofPattern("yyyy/MM/dd");

    /**
     * LocalDateTime -> String (yyyy-MM-dd HH:mm:ss)
     *
     * @param localDateTime
     * @return
     */
    public static String toString(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        return localDateTime.format(DATE_FORMATTER_4);
    }

    /**
     * LocalDate -> String (yyyy-MM-dd)
     *
     * @param localDate
     * @return
     */
    public static String toString(LocalDate localDate) {
        if (localDate == null) {
            return null;
        }
        return localDate.format(DATE_FORMATTER_5);
    }

    /**
     * LocalDate -> Long
     *
     * @param localDate
     * @return
     */
    public static Long toEpochMilli(LocalDate localDate) {
        if (localDate == null) {
            return null;
        }
        return localDate.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli();
    }

    /**
     * LocalDateTime -> Long
     *
     * @param localDateTime
     * @return
     */
    public static Long toEpochMilli(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return toEpochMilli();
        }
        return localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli();
    }

    /**
     * 当前时间 -> Long
     *
     * @return
     */
    public static Long toEpochMilli() {
        return LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli();
    }

    /**
     * Long -> String
     *
     * @param localDateTime
     * @return
     */
    public static String toString(Long localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(localDateTime), ZoneOffset.UTC);
        return toString(time);
    }

    /**
     * 比较当前时间
     *
     * @param localDate
     * @return
     */
    public static Integer beforeYear(LocalDate localDate) {
        if (localDate == null) {
            return null;
        }
        return Period.between(localDate, LocalDate.now()).getYears();
    }
}
